<script>
import { mapGetters } from 'vuex';
import PropForm from '@/views/propety/components/propForm.vue';
import PropDetail from '@/views/propety/components/propDetail.vue';

export default {
  name: 'cost',
  components: { PropDetail, PropForm },
  data() {
    return {
      /* 获取物业费列表的分页参数 */
      pageParams: {
        page: 1,
        pageSize: 10,
        start: '',
        end: '',
        enterpriseName: '',
      },
      /* 日期选择框收集的数据 */
      time: [],
      /* 加载账单状态 */
      loading: false,
      /* 是否显示添加账单的弹框 */
      dialogVisible: false,
      /* 是否是添加账单还是查看账单 */
      addFee: true,
    };
  },
  methods: {
    /* 获取物业费列表的回调函数 */
    async fetchPropertyfeeList(pageParams) {
      try {
        this.loading = true;
        await this.$store.dispatch('propetyStore/fetchPropertyfeeList', pageParams);
      } catch (error) {
        this.$message.error(error.message);
      } finally {
        this.loading = false;
      }
    },
    /* 分页器size发生变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize;
      this.fetchPropertyfeeList(this.pageParams);
    },
    /* 分页器page发生变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page;
      this.fetchPropertyfeeList(this.pageParams);
    },
    /* 点击搜索按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1;
      this.fetchPropertyfeeList(this.pageParams);
    },
    /* 日期选择框组件用户确认选定的值时触发的回调函数 */
    changeDate([start, end]) {
      this.pageParams.start = start;
      this.pageParams.end = end;
    },
    /* 点击删除按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`删除${row.enterpriseName}企业下的账单?是否继续`, '提示').catch(error => error);
      if (confirm === 'cancel') return;
      try {
        const deleteResult = await this.$store.dispatch('propetyStore/deletePropertyfee', row.id);
        this.fetchPropertyfeeList(this.pageParams);
        this.$message.success(deleteResult);
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    /* 点击添加按钮的回调函数 */
    handleAdd() {
      this.dialogVisible = true;
      this.addFee = true;
    },
    /* 点击查看按钮的回调函数 */
    handleShow(row) {
      this.dialogVisible = true;
      this.addFee = false;
      this.$nextTick(() => {
        this.$refs.propDetail.findPayInfo(row);
      });
    },
    /* 子组件添加账单成功的回调函数 */
    handleSuccess() {
      this.dialogVisible = false;
      this.fetchPropertyfeeList(this.pageParams);
    },
  },
  computed: {
    ...mapGetters('propetyStore', ['total', 'propertyfeeList']),
    dialogTitle() {
      return this.addFee ? '添加账单' : '查看账单';
    },
  },
  mounted() {
    this.fetchPropertyfeeList(this.pageParams);
  },
};
</script>

<template>
  <div class="propety">
    <el-card>
      <el-form inline>
        <el-form-item label="企业名称">
          <el-input placeholder="请输入企业名称" size="small" v-model="pageParams.enterpriseName"></el-input>
        </el-form-item>
        <el-form-item label="缴费时间">
          <el-date-picker v-model="time" @change="changeDate" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" size="small" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加账单</el-button>
      <el-table border stripe size="small" :header-cell-style="{ backgroundColor: '#f4f6f8' }" :data="propertyfeeList" v-loading="loading" element-loading-text="拼命加载中">
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="账单编号" prop="billNumber" align="center"></el-table-column>
        <el-table-column label="企业名称" prop="enterpriseName" align="center"></el-table-column>
        <el-table-column label="租赁楼宇" prop="buildingName" align="center"></el-table-column>
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" align="center"></el-table-column>
        <el-table-column label="账单金额(元)" prop="paymentAmount" align="center"></el-table-column>
        <el-table-column label="缴费时间" align="center">
          <template #default="{row}">{{ row.startTime }}--{{ row.endTime }}</template>
        </el-table-column>
        <el-table-column label="操作" prop="" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleShow(row)">查看</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 30, 40, 50]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <prop-form @success="handleSuccess" v-if="addFee"></prop-form>
      <prop-detail v-else ref="propDetail"></prop-detail>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.propety {
  padding: 15px;

  .el-card {
    margin-bottom: 10px;
  }

  .el-table {
    margin: 10px 0;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }
}
</style>
